xquery version "1.0";
declare namespace ant = "http://org.ow2.frascati.antdroid/v1";
declare namespace ns2 = "http://org.ow2.frascati.trackme/v1";

let $result := doc("results")
let $xp := doc("xp")
return
		for $x in $xp/ranking/xps//xp
		(: A modifier : mettre la date du jour à la place de 2008-05-18 :)
			let $bd_traces := doc(fn:concat(string($x/@id), "-!date!"))
			return
					insert node 
						<xp>
							{$x/@id}
							<date>!date!</date>
							<users>
							{ 
							for $user in $bd_traces//ant:traces
							return
									<user>
										{$user/@id}
										{
											for $req in $x//req
												let $o := $user//ns2:sensor[@name = $req/query/text() and $req/options/text()]
												return
												<notation>
													<nom>{$req/nom/text()}</nom>
													<points>{count($o) * $req/point/text()}</points>
												</notation>
										}
									</user>
							}
							</users>
						</xp>
						into $result/results